package com.compty.paint.freemarker;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.framework.context.webcontext.ThreadContextHolder;
import com.compty.core.framework.util.DateUtil;
import com.compty.paint.domain.Painting;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.util.Constants;

import freemarker.template.TemplateException;

/**
 * 生成静态HTML文件基类
 *
 */
public abstract class BaseGenerate {

	public Map<String,Object> root = new HashMap<String,Object>();
	private Logger logger = Logger.getLogger(BaseGenerate.class);
	
	String rootDir = Constants.GenerateHtml_Output_Directory;//html文件生成的根目录
	protected Integer curPage = 1;
	protected Integer pageSize = 10;
	private PaintingManager paintingManager;
	
	public BaseGenerate() {
		HttpServletRequest request = ThreadContextHolder.getHttpRequest();
		String ctx = request.getScheme()+"://"+request.getServerName()+request.getContextPath();;
		root.put("ctx",ctx);
		this.paintingManager = SpringContextHolder.getBean("paintingManager");
	}

	public void output(String template_name,String sGeneFilePath,String sFileName) throws TemplateException, IOException{
		FreeMarkerUtil freeMarkerUtil = FreeMarkerUtil.getInstance();
		
		boolean bOK = freeMarkerUtil.geneHtmlFile(rootDir,template_name,root,sGeneFilePath, sFileName);
		
		if(bOK){
			logger.info(DateUtil.toString(DateUtil.getNow(), "yyyy-MM-dd HH:mm:ss")+sGeneFilePath+"/"+sFileName+"生成成功。");
		}else{
			logger.info(DateUtil.toString(DateUtil.getNow(), "yyyy-MM-dd HH:mm:ss")+sGeneFilePath+"/"+sFileName+"生成失败。");
		}
	}
	
	protected void dealNavigateUrl(Painting painting) {
		if( painting.getPainter() != null ){
			Long painterId = painting.getPainter().getId();
			List<Painting> paintingList = paintingManager.findAvailablePaintingsByPainter(painterId);
			int len = paintingList.size();
			int position = 0;
			for (int i = 0; i < len; i++) {
				if( painting.getId() == paintingList.get(i).getId() ){
					position = i;
				}
			}
			if(len > 1){
				if( position == 0){
					root.put("preUrl","");
					root.put("nextUrl",paintingList.get(position+1).getLinkUrl());
				}
				else if( position == len-1){
					root.put("preUrl",paintingList.get(position-1).getLinkUrl());
					root.put("nextUrl","");
				}else{
					root.put("preUrl",paintingList.get(position-1).getLinkUrl());
					root.put("nextUrl",paintingList.get(position+1).getLinkUrl());
				}
			}else{
				root.put("preUrl", "");
				root.put("nextUrl", "");
			}
		}else{
			root.put("preUrl", "");
			root.put("nextUrl", "");
		}
	}	
	
	protected void getImagesArray(Painting painting){
		StringBuffer sb = new StringBuffer("[");
		if(painting.getImageInfo() != null){
			sb.append("{originalImagePath:\""+painting.getOriginalImagePath()+"\",middleImageBpath:\""+painting.getMiddleImageBpath()+"\",smallImagePath:\""+painting.getSmallImagePath()+"\"}");
		}
		if(painting.getImageInfo2() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath2()+"\",middleImageBpath:\""+painting.getMiddleImageBpath2()+"\",smallImagePath:\""+painting.getSmallImagePath2()+"\"}");
		}
		if(painting.getImageInfo3() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath3()+"\",middleImageBpath:\""+painting.getMiddleImageBpath3()+"\",smallImagePath:\""+painting.getSmallImagePath3()+"\"}");
		}
		if(painting.getImageInfo4() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath4()+"\",middleImageBpath:\""+painting.getMiddleImageBpath4()+"\",smallImagePath:\""+painting.getSmallImagePath4()+"\"}");
		}
		sb.append("]");
		root.put("imagesArray", sb.toString());
	}
	
	public Integer getCurPage() {
		return curPage;
	}

	public void setCurPage(Integer curPage) {
		this.curPage = curPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public abstract void generateHtml() throws TemplateException, IOException;
	
}
